package com.ymatou.iapi.productsync.testcase;

import com.shop2cn.iapi.activitymgt.parameter.AddactivityBean;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymatou.iapi.productsync.parameter.CataLogDto;
import com.ymatou.iapi.productsync.parameter.ExtParamBean;
import com.ymatou.iapi.productsync.parameter.InvokemongocrudBean;
import com.ymatou.iapi.productsync.service.InvokemongocrudCall;
import com.ymatou.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;

import org.junit.*;
import org.junit.experimental.categories.Category;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * mongocrudTest FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Invokemongocrud {
	private static InvokemongocrudBean invokemongocrudBean;
	private static InvokemongocrudCall invokemongocrudCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("mongocrudTest");
	}

	@Before
	public void caseUp() {
		invokemongocrudBean = new InvokemongocrudBean();
		invokemongocrudCall = new InvokemongocrudCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(invokemongocrudCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}


	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_Invokemongocrud_001() {
		Logger.start(true, "正确数据，成功");
		try {
			Integer rootUserId = GodmchManageCallService.CreateGodMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					"", false);
			double price = 200;
			addAgentProductBean.getCatalogList().forEach(a -> {
				a.getAgentPriceList().forEach(b -> {
					b.setAgentPrice(price);
				});
				a.setDirectPrice(price);
			});
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(rootUserId,
					productId);
			// 创建限时抢购活动
			List<String> catalogList = new ArrayList<String>();
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});
			
			
			List<CataLogDto> catalog=new ArrayList<>();
			AddactivityBean initProductaddactivity = AgentProductCallService.initProductaddactivity(rootUserId,
					productId, catalogList);
			initProductaddactivity.getProductList().forEach(a -> {
				a.getCatalogList().forEach(b -> {
					CataLogDto cataLogDto = new CataLogDto();
					cataLogDto.setCatalogId(b.getCatalogId());
					cataLogDto.setDiscount(0.9);
					cataLogDto.setDiscountPrice(99);
					catalog.add(cataLogDto);
					b.setDiscount(new BigDecimal("2"));
				});
			});
			int activityId = AgentProductCallService.agentProductaddactivity(initProductaddactivity);

			
			ExtParamBean extParamBean = new ExtParamBean();
			extParamBean.setActivityId(activityId);
			extParamBean.setName(initProductaddactivity.getName());
			extParamBean.setActivityType(initProductaddactivity.getActivityType());
			extParamBean.setLimitType(initProductaddactivity.getLimitType());
			extParamBean.setLimitNum(initProductaddactivity.getLimitNum());
//			extParamBean.setActivityProductId(initProductaddactivity.pr);
			extParamBean.setProductId(productId);
//			extParamBean.setMerchantId(initProductaddactivity.getme);
//			extParamBean.setBeginTime(DateFormatUtil.formatDate("yyyy-MM-dd'T'HH:mm:ss", initProductaddactivity.getBeginTime()));
//			extParamBean.setEndTime(DateFormatUtil.formatDate("yyyy-MM-dd'T'HH:mm:ss", initProductaddactivity.getEndTime()));
			extParamBean.setDiscountType(1);
			extParamBean.setCatalogList(catalog);
			
			invokemongocrudBean.setActionType("AddAgentActivityProduct");
			invokemongocrudBean.setAppId("appId");
			invokemongocrudBean.setExtParam(FormatUtil.beanToGSONString(extParamBean));
			invokemongocrudBean.setProductId(productId);

			invokemongocrudCall.setData(invokemongocrudBean);
			invokemongocrudCall.callService();
			Logger.verifyEquals("200", invokemongocrudCall.getString("code"), "验证code");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}